package java6681.generic;

import java.util.*;

/**
 * @author Junichiro
 * @description Map集合的两种遍历方法
 * @date 2021-04-11 10:05
 **/

public class MapForeach6681 {

    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("1", "壹");
        map.put("2", "贰");
        map.put("3", "叁");
        map.put("4", "肆");
        map.put("5", "伍");
        map.put("4", "壹");
        System.out.println(map);
        System.out.println(map.containsKey("4"));
        System.out.println(map.get("3"));
        System.out.println(map.remove("2"));
        System.out.println(map.remove("2"));
        System.out.println(map);
        /*Set集合*/
        print6681(map);
        /*Map.Entry<K,V>集合*/
        Set<Map.Entry<String, String>> entries = map.entrySet();
        print(entries);
    }

    /**
     * 功能：利用Key的Set集合遍历Map集合的方法
     *
     * @param hm Map集合
     */
    private static void print6681(Map<String, String> hm) {
        Set<String> keys = hm.keySet();
        for (String key : keys) {
            System.out.println(key + "," + hm.get(key));
        }
    }

    /**
     * 功能：利用Key的Set集合遍历Map集合的方法
     *
     * @param entry Set<Map.Entry<K,V>>集合
     */
    private static void print(Set<Map.Entry<String, String>> entry) {
        for (Map.Entry<String, String> ssEntry : entry) {
            System.out.println(ssEntry.getKey() + "," + ssEntry.getValue());
        }
    }
}
